	import safeData from '@/components/fieldSampling/safeList';
	import loading from '@/components/xuan-loading/xuan-loading.vue';

	import {
		log
	} from 'util'

	export default {

		data() {
			return {
				pagesItem: {
					page: 1,
					pageSize: 20,
				},
				pageTotal: null,
				Inv: 0,
				datasList: {
					fCompanyid: "", // 施工标段id
					startDates: "", // 开始时间
					endDates: "", // 截至时间
					strengthgrade: "", // 强度等级
					stationId: "", // 拌合站id
					machineId: "", // 拌合机编号
					warningLevel: "", // 预警等级
					fCloseoperation: "", // 闭合状态
					keyWord: "", // 工程部位/工程名称 模糊条件
					page: "1", // 当前页码
					pageSize: "20", // 每页显示条数
					fCheckStatus: "0", //判断级别
					modelType: '', //0待办  1已办
				},

				// 加载
				width: '150rpx',
				height: '100rpx',
				shadeShow: true,
				backgroundColor: "transparent",
				// 加载

				// 新增
				num: 0,
				userInfo: null,
				//列表数据
				unSubmitData: [],

				fCheckStatus: null, // 0 施工单位 1监理  2业主 3完成


				// 根据条件查询
				checked: "",
				selectParent: false,
				multiple: false,
				companyTree: [], //调接口获取树结构
				flod: true,
				defaultProps: {
					children: "children",
					label: "fShortname",
				},
				treeData: [],
				// 根据条件查询

			}
		},
		components: {
			safeData,
			loading,
		},
		onShow() {
			var that = this

			if (this.Inv == 0) {
				setTimeout(() => {
					that.unsubmit()

				}, 300)
			}
			if (this.Inv == 1) {
				setTimeout(() => {
					that.submit()

				}, 300)
			}

		},
		onLoad(option) {

			var that = this;
			uni.getStorage({ //监督检查方式
				key: 'userInfo',
				success: function(res) {
					if (option.Inv) {
						that.Inv = option.Inv
					}
					that.userInfo = JSON.parse(res.data);
					that.getListData(that.Inv.toString());
				}
			});
		},


		onBackPress(event) {
			uni.switchTab({
				url: '/pages/home/index'
			});
			return true;
		},
		onNavigationBarButtonTap(e) {
			// console.log(e)

			uni.navigateTo({
				url: '/pages/apply/sampleManage/fieldSampling/inter/index?type=' + 'add'
			});
			return true;
		},
		onReachBottom() {
			if (this.pagesItem.page >= this.pageTotal) return
			this.pagesItem.page++
			this.getListData();
		},
		methods: {
			treeCancel() {
				this.checked = ""
			},
			treeCancel() {},
			TreeList() {
				// this.$refs.loadings.open();
				this.$http.getUserCompanyTreeList({
					companyId: this.userInfo.companyId,
					userId: this.userInfo.userId
				}).then(res => {
					if (res.errcode == 0) {
						this.treeData = this.tranListToTreeData(res.data.userCompanyTreeList);
						this.companyTree = this.treeData;
						// this.$refs.loadings.close();
					} else {
						uni.showToast({
							title: "获取机构树失败",
							icon: 'none',
							duration: 2000
						});
					}
				}, (err) => {
					uni.showToast({
						title: "接口报错",
						icon: 'none',
						duration: 2000
					});
					// this.$refs.loadings.close();
				})
			},
			tranListToTreeData(data) {
				let result = [];
				if (!Array.isArray(data)) {
					return result;
				}
				// 清除已存在的children 重新赋值
				data.forEach((item) => {
					delete item.children;
				});

				let map = {};
				data.forEach((item) => {
					map[item.fCompanyid] = item;
				});

				data.forEach((item) => {
					let parent = map[item.fParentid];
					if (parent) {
						(parent.children || (parent.children = [])).push(item);
					} else {
						result.push(item);
					}
				});
				return result;
			},

			// 跳详情页
			safeDetail(e) {
				// console.log("跳转", e);
				// this.fCheckStatus = e.id;
				if (e.Inv == 0) {
					uni.navigateTo({
						url: "/pages/apply/sampleManage/fieldSampling/inter/index?id=" + e.id
					});
					return false;
				} else if (e.Inv == 1) {
					uni.navigateTo({
						url: "/pages/apply/sampleManage/fieldSampling/submit/index?id=" + e.id
					});
					return false;
				}
			},
			// 跳详情页
			submit() {
				var that = this
				this.Inv = 1;
				this.unSubmitData = [];
				this.pagesItem.page = 1;
				this.getListData();
			},
			unsubmit() {
				var that = this
				this.Inv = 0;
				this.unSubmitData = [];
				this.pagesItem.page = 1;
				this.getListData();
			},

			callback() {},
			getListData(num) {
				var that = this;

				let obj = {
					fCompanyid: this.userInfo.companyId, // 左侧选中组织机构id
					userId: this.userInfo.userId, // 查询待办或已办时传 列表页查询不传
					fSamplemodel: "", // 上方查询条件样品种类  对应code
					fSamplename: "", // 上方查询条件样品名称
					fStatus: "", // 上方查询条件样品状态
					startDates: "", //取样日期开始时间
					endDates: "", //取样日期结束时间
					modelType: this.Inv.toString(), // 取样：0-待办 1-已办;收样：3-待办 4-已办；查看：传2
					page: this.pagesItem.page, // 当前页码
					pageSize: this.pagesItem.pageSize, // 每页显示条数
				}
				// this.$refs.loadings.open();
				this.$http.getSampleWorkWebList(obj).then(res => {
					if (res.errcode == 0) {
						// console.log("列表", res)
						that.pageTotal = res.data.pageInfo.pages;
						that.unSubmitData = [...that.unSubmitData, ...res.data.pageInfo.list];
						// this.$refs.loadings.close();
					} else {
						uni.showToast({
							title: "获取数据失败",
							icon: 'none',
							duration: 2000
						});
						// this.$refs.loadings.close();
					}
				}, (err) => {
					uni.showToast({
						title: "获取数据失败",
						icon: 'none',
						duration: 2000
					});
					this.$refs.loadings.close();
				});
			},

			callback() {

			},
		}
	}